# -*- coding: UTF-8 -*-
# @Time : 2021/12/23 0023 13:44
# @Author : lyw
# @Remark : 普通空调控制DB
import logging

from mxsoftpy.exception import DBError

from db.public.IOTManager.iot_base import IOTBaseDB
from utils.classes.custom_exception import VerifyError
from index import _


class ACSelectDB(IOTBaseDB):
    device_type = "ModbusAC"
    query_field = ["mxlabel", "mxdevicetype", "mxparent", "isdel", "_target_ip", "_port", "_Addr"]

    def query_table(self, code):
        """
        获取告警记录
        @param code:
        @return:
        """
        try:
            record_list = self.table.open("MXSE", "%s.SubMonitor.modbus_ac_rs_ktc_n01_temp_" % code).record_by_time(
                minutes=5, count=1
            )
            if record_list:
                return record_list[0]
            return dict()
        except DBError as e:
            logging.error(e)
            raise VerifyError(_("查询失败"))

    def param_middle(self, data_obj) -> dict:
        """
        基础查询 参数
        @param data_obj:
        @return:
        """
        return {"iot_code": data_obj.iot_code, "device_type": data_obj.device_type}


class ACUpdateDB(IOTBaseDB):

    def retrieve(self, code):
        """
        查询设备
        @return:
        """
        try:
            return self.tree.open("MXSE", "1\\1.SD\\%s" % code, file="ccubase").items()
        except DBError as e:
            logging.error(e)
            raise VerifyError(_("查询失败"))


class ACSetDB(ACUpdateDB):
    pass


class ACRetrieveDB(ACUpdateDB):
    pass
